package algorithm.leetcode.array;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: util
 * @description: 数组中的元素可以无限使用，
 * 计算有多少种方式使数组元素之和为target
 * @author: zhouliangliang
 * @create: 2021-05-20 20:14
 **/
public class SumArray {

    public void add(int []array, int target, List<Integer> current, int start, List<List<Integer>> result) {

        if(target > 0) {
            for(int i=0; i<array.length; i++) {
                if(array[i] > target) {
                    continue;
                }
                current.add(array[i]);
                add(array, target-array[i], current, i, result);
                current.remove(current.size()-1);

            }
        }
        if(target == 0) {
            result.add(new ArrayList<>(current));
        }
    }

    private void cal(int []array, int target) {
        List<List<Integer>> result = new LinkedList<>();

        add(array, target, new ArrayList<>(), 0, result);

        result.stream().forEach(item->{
            System.out.println(Joiner.on(",").join(item));
        });
    }

    public static void main(String[] args) {
        new SumArray().cal(new int[]{2,3,5}, 8);
    }

}
